package com.itrui.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itrui.comment.pojo.Blacklist;
import com.itrui.comment.pojo.Comment;
import com.itrui.comment.service.BlacklistService;
import com.itrui.comment.service.CommentService;
import com.itrui.comment.mapper.CommentMapper;
import com.itrui.feign.client.PictureClient;
import com.itrui.feign.client.common.Picture;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 蕊蕤的小腿肚子
 * @description 针对表【comment(评论)】的数据库操作Service实现
 * @createDate 2022-08-17 22:28:54
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {

    @Autowired
    private BlacklistService blacklistService;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private PictureClient pictureClient;

    @Override
    public boolean isBlack(Long sender, Long target) {

        LambdaQueryWrapper<Blacklist> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Blacklist::getUserId, target).eq(Blacklist::getTarget, sender);

        if (blacklistService.getOne(queryWrapper) != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean delete(Long id, int type) {
        //删除评论
        int i = commentMapper.deleteById(id);

        //删除评论图片
        boolean b = pictureClient.deletePicture(id, 2);

        return i > 0 && b ? true : false;
    }

    @Override
    public List<Comment> get(Long id, int type) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getTarget,id);

        List<Comment> comments = commentMapper.selectList(queryWrapper);
        comments = comments.stream().map((item) ->{
            List<Picture> pictures = pictureClient.getPicture(item.getId(), 2);
            item.setPictures(pictures);
            return item;
        }).collect(Collectors.toList());

       return comments;


    }

}




